package com.hyt.it.ogt.kq.service.gov.websocket;


import com.hyt.it.ogt.kq.service.gov.service.IProgressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

/**
 * 初始化试卷
 *
 * @author 刘恒活
 * @since 2020/7/14 9:11
 */
@ServerEndpoint("/kw/gov/ws/progress/{taskId}/{progressType}")
@Component
@Slf4j
public class ProgressWebsocket {
    private static IProgressService iProgressService;

    public static void setIProgressService(IProgressService iProgressService) {
        ProgressWebsocket.iProgressService = iProgressService;
    }

    /**
     * 连接建立成功调用的方法
     *
     * @param session
     * @param taskId
     */
    @OnOpen
    public void onOpen(Session session, @PathParam("taskId") String taskId,
                       @PathParam("progressType") Integer progressType) throws Exception {
        iProgressService.getProgressForWebsocket(session, taskId, progressType);
    }

    /**
     * 连接关闭调用的方法
     *
     * @param session
     */
    @OnClose
    public void onClose(Session session) {
        log.info("# websocket连接关闭调用:{}", session.getId());
    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param message
     * @param session
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        log.info("# 接收前端消息:【{}】", message);
    }

    /**
     * 出现错误
     *
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        log.error("# websocket出现错误：{}，Session ID： {}", error.getMessage(), session.getId());
    }

}
